package com.qcheng.diangpingclient;

import java.util.ArrayList;
import java.util.List;

import com.example.diangpingclient.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

/**
 * 页面滑动效果
 * 
 * @author 伊斯坦堡
 * 
 */
public class WelconGuideAct extends Activity {
	@ViewInject(R.id.welcome_guide_btn)
	private Button btn;
	@ViewInject(R.id.welcome_paper)
	private ViewPager paper;
	private List<View> list;// 定义一个List集合放三张图片

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.welcome_guide);
		// xutils初始化
		ViewUtils.inject(this);
		// 初始化ViewPager方法
		initViewPager();
	}

	@OnClick(R.id.welcome_guide_btn)
	public void click(View view) {
		startActivity(new Intent(getApplicationContext(), MainActivity.class));
	}

	/**
	 * 初始化ViewPager方法
	 */
	public void initViewPager() {
		list = new ArrayList<View>();
		ImageView iv1 = new ImageView(this);
		iv1.setImageResource(R.drawable.guide1);
		list.add(iv1);

		ImageView iv2 = new ImageView(this);
		iv2.setImageResource(R.drawable.guide2);
		list.add(iv2);

		ImageView iv3 = new ImageView(this);
		iv3.setImageResource(R.drawable.guide3);
		list.add(iv3);

		paper.setAdapter(new MyPagerAdapter());
		// 监听ViewPager滑动效果
		paper.setOnPageChangeListener(new OnPageChangeListener() {
			// 页卡被选中的方法
			@Override
			public void onPageSelected(int arg0) {
				// 如果是第三个页面
				if (arg0 == 2) {
					btn.setVisibility(View.VISIBLE);
				} else {
					btn.setVisibility(View.GONE);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	/**
	 * 自定义ViewPagerAdapter
	 * 
	 * @author 伊斯坦堡
	 * 
	 */
	class MyPagerAdapter extends PagerAdapter {
		/**
		 * 计算图片的格数
		 */
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		/**
		 * 初始化Item实例方法
		 */
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(list.get(position));
			return list.get(position);
		}

		/**
		 * Item销毁的方法
		 */
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// super.destroyItem(container, position, object);
			container.removeView(list.get(position));
		}

	}
}
